package com.jp.knowledge.my.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.google.gson.JsonObject;
import com.jiayen.cache.FileCacheManager;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.adapter.IntegralAdapter;
import com.jp.knowledge.my.model.IntegralModel;
import com.jp.knowledge.util.ResultSubscriber;
import com.jp.knowledge.view.JpDiver;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by 528787067@qq.com on 2017/5/5.
 */

public class IntegralActivityOld extends SlidingActivity implements View.OnClickListener, CanRefreshLayout.OnRefreshListener, ResultSubscriber.OnResultListener {

    @ViewInject(R.id.integralNum)
    private TextView integralNum;
    @ViewInject(R.id.integralEarn)
    private TextView integralEarn;
    @ViewInject(R.id.integralMall)
    private TextView integralMall;
    @ViewInject(R.id.refresh)
    private CanRefreshLayout refreshLayout;
    @ViewInject(R.id.can_content_view)
    private RecyclerView recyclerView;

    private List<IntegralModel.ListBean> integralModelList;
    private IntegralAdapter integralAdapter;
    private String cachePath;
    private FileCacheManager cacheManager;

    @Override
    protected int getResourceId() {
        return R.layout.activity_integral_old;
    }

    @Override
    protected void init() {
        initTitleBar();
        integralEarn.setOnClickListener(this);
        integralMall.setOnClickListener(this);
        refreshLayout.setLoadMoreEnabled(false);
        refreshLayout.setOnRefreshListener(this);
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(new JpDiver(mContext));
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        integralModelList = new ArrayList<>();
        integralAdapter = new IntegralAdapter(mContext, integralModelList);
        recyclerView.setAdapter(integralAdapter);

        integralNum.setText(application.getUserData().getIntegralNum() + "");

        cachePath = mContext.getCacheDir() + "/integral";
        if(application.getUserData() != null)
            cachePath += ("_" + application.getUserData().getUuid());
        cachePath += ".dat";
        cacheManager = new FileCacheManager();
        readCache();
    }

    private void initTitleBar(){
        topName.setText("我的积分");
        rightIcon.setVisibility(View.GONE);
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        leftIcon.setOnClickListener(this);
    }

    private void readCache(){
        Observable.create(new Observable.OnSubscribe<List<IntegralModel.ListBean>>() {
            @Override
            public void call(Subscriber<? super List<IntegralModel.ListBean>> subscriber) {
                List<IntegralModel.ListBean> data = (List<IntegralModel.ListBean>) cacheManager.readObject(cachePath);
                subscriber.onNext(data);
            }
        }).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Subscriber<List<IntegralModel.ListBean>>() {
            @Override
            public void onCompleted() {
                refreshLayout.autoRefresh();
            }
            @Override
            public void onError(Throwable e) {
                onCompleted();
            }
            @Override
            public void onNext(List<IntegralModel.ListBean> data) {
                if(data != null) {
                    integralModelList = data;
                    integralAdapter.updateAdapter(integralModelList);
                }
                onCompleted();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.icon_left:
                finish();
                break;
            case R.id.integralEarn:
                break;
            case R.id.integralMall:
                break;
        }
    }

    @Override
    public void onRefresh() {
        JsonObject param = new JsonObject();
        param.addProperty("time", System.currentTimeMillis());
        HttpHelper.getInstance(mContext).integralHistory(param, 0x01, this);
    }

    @Override
    public void onStart(int requestType) {}
    @Override
    public void onCompleted(int requestType) {
        refreshLayout.refreshComplete();
    }
    @Override
    public void onError(int requestType) {
        onCompleted(requestType);
    }

    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0){
            ToasUtil.toast(mContext, data.getMessage());
            onError(requestType);
        } else if(requestType == 0x01){
            integralModelList.clear();
            List<IntegralModel> integralModels = data.getList(IntegralModel.class);
            if(integralModels != null && integralModels.size() > 0){
                for(IntegralModel integralModel : integralModels){
                    IntegralModel.ListBean listBean = new IntegralModel.ListBean();
                    listBean.setTitle(integralModel.getDateTime()+"");
                    integralModelList.add(listBean);
                    List<IntegralModel.ListBean> listBeen = integralModel.getList();
                    if(listBeen == null || listBeen.size() == 0)
                        continue;
                    for(IntegralModel.ListBean bean : listBeen)
                        integralModelList.add(bean);
                }
            }
            integralAdapter.updateAdapter(integralModelList);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    cacheManager.wirteObject(integralModelList, cachePath);
                }
            }).start();
        }
    }
}
